<?php
require_once 'data_form'.EXT;

class education extends data_form{

	function __construct(){
		parent::__construct();
		$this->load->model(array('education_model','explanation_model','current_holding_model'));
	}
	
	function index()
	{
		$this->data['module_title']	=	'Education';
		$row	=	null;
		
		$row	=	$this->education_model->get_many_by(array('customer_id' => $_SESSION['customer']['id']));
		$exp	=	$this->explanation_model->get_by(array('customer_id' => $_SESSION['customer']['id'], 'data_form_id' => 3));
		if($_SERVER["REQUEST_METHOD"] == "GET" && $row)
		{
			foreach($row as $v){
				$temp	=	array(
					'customer_id'		=>	$v->customer_id,
					'gender'			=>	$v->gender,
					'date_of_birth'		=>	$v->date_of_birth,
					'years_to_university'	=>	$this->education_model->Years_to_University($v->date_of_birth,$v->gender),
					'id'				=>	$v->id
				);
				$data[]	=	$temp;
			}
			$_SESSION['data']	=	$data;
		}
		
		if($_SERVER["REQUEST_METHOD"]  == "POST")
		{
			
			$submit	=	$this->input->post('submit');
			$chil	=	$this->input->post('child');
			if(!$submit){
				unset($data);
				
				for($i=1; $i<=$chil; $i++){
					
					$temp	=	array(
						'customer_id'		=>	$_SESSION['customer']['id'],
						'gender'			=>	$_POST['select'.$i],
						'date_of_birth'		=>	convert_date_mysql($_POST['birth'.$i]),
						'years_to_university'	=>	$this->education_model->Years_to_University(convert_date_mysql($_POST['birth'.$i]),$_POST['select'.$i]),
						'id'				=>	$_POST['id'.$i]
					);
					$data[]	=	$temp;
				}
				$_SESSION['data']	=	$data;
			}
			else 
			{
				if($exp){
					$this->explanation_model->update($exp->id,array('explanation' => $_POST['textfield3']));
				}
				else{
					$this->explanation_model->insert(array('explanation' => $_POST['textfield3'], 'customer_id' => $_SESSION['customer']['id'], 'data_form_id' => 3));
				}
				unset($data);
				$data	=	$_SESSION['data'];
				$arr_id=array();
				foreach($data as $k => $d)
				{
					$d['explanation']	=	$this->input->post('textfield3');
					if($d['id'] == '')
					{
						unset($d['id']);
						$id	=	$this->education_model->insert($d);
					}
					else{
						$id	=	$d['id'];
						unset($d['id']);
						if($this->education_model->update($id,$d)){
							echo 'ok';
						}
					}
					$_SESSION['data'][$k]['id'] = $id;
					$arr_id[]=$id;
				}
				
				if($arr_id){
					$row_del	=	$this->db->select()->from('educations')->where_not_in('id',$arr_id)->where('customer_id',$_SESSION['customer']['id'])->get()->result();
					if($row_del){
						foreach($row_del as $v){
							$this->education_model->delete($v->id);
						}
					}
				}
				
				//close form
				echo '<script>window.close();</script>';
				//
			}
		}
		$this->data['exp']	=	$exp;
		$this->data['row']	=	(isset($_SESSION['data']) ? $_SESSION['data'] : null);
		$this->load->view("education/index", $this->data);
	}
	
	function ss($a1=array(),$a2=array())
	{
		$temp=null;
		foreach($a1 as $v1){
			$ok=false;
			foreach($a2 as $v2){
				if($a1['id']==$a2['id']){$ok=true;break;}
			}
			if($ok==false){
				$temp[]=$v1;
			}
		}
		return $temp;
	}
	
	function like($a1,$a2)
	{
		$temp=null;
		foreach($a1 as $v1){
			foreach($a2 as $v2){
				if($a1['id']==$a2['id']){$temp[]=$v1;break;}
			}
		}
		return $temp;
	}
	
	function year_university()
	{
		$year_birth	=	$this->input->post('year_birth');
		$gender		=	$this->input->post('gender');
		$year_university	=	$this->education_model->Years_to_University($year_birth,$gender);
		die(''.$year_university);
	}

}